package cn.itcast.hotel;

import cn.itcast.hotel.pojo.HotelSDEDoc;
import cn.itcast.hotel.repository.HotelRepository;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@SpringBootTest
public class SpringDataEsManager {
    @Autowired
    private HotelRepository hotelRepository;
    @Test
    public void testAddHotelSDEDoc(){
        HotelSDEDoc hotelSDEDoc = new  HotelSDEDoc(1L,"顺义如家",300,"如家","北京");
        hotelRepository.save(hotelSDEDoc);
        List<HotelSDEDoc> list = new ArrayList<>();
        list.add(new HotelSDEDoc(1L, "顺义如家",  299, "如家","北京"));
        list.add(new HotelSDEDoc(2L, "海淀汉庭",  299,"汉庭", "北京"));
        list.add(new HotelSDEDoc(3L, "桥头汉庭", 199,"汉庭", "天津"));
        list.add(new HotelSDEDoc(4L, "滨海如家",  199,"如家", "天津"));
        list.add(new HotelSDEDoc(5L, "上海喜来登",  2000, "喜来登","上海"));
        hotelRepository.saveAll(list);

    }
    @Test
    public void testUpdateHotelSDEDoc(){
        HotelSDEDoc hotelSDEDoc = new  HotelSDEDoc(1L,"顺义如家22222",300,"如家","北京");
        hotelRepository.save(hotelSDEDoc);
    }

    @Test
    public void testFindCondition(){
        List<HotelSDEDoc> hotelList =  hotelRepository.findByNameAndBrand("顺义","如家");
        System.out.println(hotelList);
    }

    @Test
    public void testDeleteHotelSDEDoc(){
        hotelRepository.deleteById(1L);
        hotelRepository.deleteAll();
    }

    @Test
    public void testRepositoryAndRestClient(){
        QueryBuilder query = QueryBuilders.matchQuery("name","如家");
        Pageable pageable = PageRequest.of(0,3,Sort.by(Sort.Direction.DESC,"price"));
        Iterable<HotelSDEDoc> hotelIterable = hotelRepository.search(query,pageable);
        for (HotelSDEDoc hotel : hotelIterable) {
            System.out.println(hotel);
        }
    }

    @Test
    public void testQueryHotelSDEDoc(){
        Optional<HotelSDEDoc> hotelSDEDocOptional = hotelRepository.findById(1L);
        HotelSDEDoc hotelSDEDoc = hotelSDEDocOptional.get();
        System.out.println(hotelSDEDoc);

        System.out.println("==================================");
        Iterable<HotelSDEDoc> hotelSDEDocIterable = hotelRepository.findAll();
        for (HotelSDEDoc hotelSDEDoc1 : hotelSDEDocIterable) {
            System.out.println(hotelSDEDoc1);
        }
        System.out.println("------------------------------------");
        Pageable pageable = PageRequest.of(0,3, Sort.by(Sort.Direction.DESC,"price"));
        Page<HotelSDEDoc> hotelSDEDocPage = hotelRepository.findAll(pageable);
        List<HotelSDEDoc> content = hotelSDEDocPage.getContent();
        for (HotelSDEDoc hotelSDEDoc1 : content) {
            System.out.println(hotelSDEDoc1);
        }
    }
}


